#include "LoginMessage.h"


LoginMessage::LoginMessage(BYTE* _buffer)
	: PipeMessage(PipeMessage::LOGIN)
{
	if( _buffer)
	{
		mBuffer = _buffer;
		ParseBuffer();
	}
}


UINT LoginMessage::GetBufferSize()
{
	return sizeof(mLoginData);
}

UINT LoginMessage::CreateBuffer( UINT _startPos /*= 0*/ )
{
	memcpy_s(mBuffer + _startPos, sizeof(mLoginData), &mLoginData, sizeof(mLoginData));
	_startPos +=  sizeof(mLoginData);

	return _startPos;
}

void LoginMessage::ParseBuffer()
{
	memcpy_s(&mLoginData, sizeof(mLoginData), mBuffer + sizeof(MessageType),  sizeof(mLoginData));
}

bool LoginMessage::SetData( string _username, string _pass, string _clientPipeName )
{
		if( _username.size() > sizeof(mLoginData.username) || _pass.size() > sizeof(mLoginData.password) )
			return false;

		strcpy_s(mLoginData.username, sizeof(mLoginData.username),_username.c_str());
		strcpy_s(mLoginData.password, sizeof(mLoginData.password),_pass.c_str());
		strcpy_s(mLoginData.clientPipeName, sizeof(mLoginData.clientPipeName),_clientPipeName.c_str());

		mBufferChanged = true;

		return true;
}

void LoginMessage::SetResult( int _result, bool _isAdmin /*= false*/ )
{
	mLoginData.result = _result;
	mLoginData.isAdmin = _isAdmin;

	mBufferChanged = true;
}
